package com.example.donghuihui.androidframe.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.example.donghuihui.androidframe.R;
/**
 * 类型选择V
 * 可以做历史记录view
 */
public class TypeSelectView extends ViewGroup implements View.OnClickListener{
    private static final int PADDING_HOR = 15;//水平方向padding
    private static final int PADDING_VERTICAL = 20;//垂直方向padding

    private OnChildSelectChangedListener mOnClickSearchListener;


    public TypeSelectView(Context context) {
        super(context);
    }

    public TypeSelectView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TypeSelectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addView(View child) {
        if (child instanceof Button) {
            Button child1 = (Button) child;
            child1.setTextColor(getResources().getColorStateList(R.color.color_btn_white));
            child1.setBackgroundResource(R.drawable.btn_gray_search);
//            child1.setButtonDrawable(android.R.color.transparent);
            child1.setPadding(30,0,30,0);
            child1.setOnClickListener(this);
        }else if (child instanceof TextView){
            TextView childText = (TextView) child;
            childText.setTextColor(getResources().getColorStateList(R.color.textcolor5));
            childText.setBackgroundResource(R.drawable.btn_gray_search);
//            child1.setButtonDrawable(android.R.color.transparent);
            childText.setPadding(30,22,30,22);
            childText.setOnClickListener(this);
        }


        super.addView(child);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int x = 0;// 横坐标开始
        int y = 0;//纵坐标开始
        int specWidth = r - l;
        x = l;
        int childCount = getChildCount();
        for(int index = 0;index<childCount;index++) {
            View childView = getChildAt(index);
            childView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int width = childView.getMeasuredWidth();
            int height = childView.getMeasuredHeight();
            x += width;
            if (x <= specWidth) {
                x += PADDING_HOR;
            } else {
                x = 0;
                x += (width + PADDING_HOR*2.5);
                y += (height+PADDING_VERTICAL);
            }
            childView.layout(x - width, y, x, y +height);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int x = 0;//横坐标
        int y;//纵坐标
        int rows = 1;//总行数
        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
        int childCount = getChildCount();

        int height = 0;
        for(int index = 0;index<childCount;index++){
            View child = getChildAt(index);
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            int width = child.getMeasuredWidth();
             height = child.getMeasuredHeight();
            x += width;
            if (x > specWidth) {//换行
                x = width;
                rows++;
            } else {
                x += PADDING_HOR;
            }
        }
        y = rows * height + (rows - 1) * PADDING_VERTICAL;

        setMeasuredDimension(specWidth, y);
    }

    public void setOnChildSelectChangedListener(OnChildSelectChangedListener listener){
        mOnClickSearchListener = listener;
    }

    @Override
    public void onClick(View view) {
        if (view instanceof Button){
            Button btn = (Button) view;
            String clickText = btn.getText().toString();
            mOnClickSearchListener.onClick(clickText);
        }else if (view instanceof TextView){
            TextView btn = (TextView) view;
            String clickText = btn.getText().toString();
            mOnClickSearchListener.onClick(clickText);
        }

    }

    public interface OnChildSelectChangedListener{
        public void onClick(String textString);
    }
    /**
     * 获取选中单选按钮的Text
     * @return
     */
    public String getChildText(){
        int childCount = getChildCount();
        for (int i=0;i<childCount;i++){
            View child = getChildAt(i);
            if (child instanceof Button) {
                return ((Button)child).getText().toString();
            }else if (child instanceof TextView){
                return ((TextView)child).getText().toString();
            }
        }
        return null;
    }

}
